
package com.sailing.lianxi.thread;

import java.util.concurrent.CountDownLatch;
public class CountDownLatchTest {
	public static void t1() {
		
		int threadNum = 2;
		final CountDownLatch latch = new CountDownLatch(threadNum);
		for(int i=0;i<threadNum;i++) {
			new Thread() {
				public void run() {
						try {
							System.out.println("子线程"+Thread.currentThread().getName()+" 正在执行");
							Thread.sleep(3000);
							System.out.println("子线程"+Thread.currentThread().getName()+" 执行结束");
							latch.countDown();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				};
			}.start();
		}
		
		try {
			System.out.println("等待"+threadNum+"个子线程执行完毕...");
			latch.await();
			System.out.println(threadNum+"个子线程已经执行完成");
			System.out.println("主线程继续执行");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) {
		t1();
	}
   
  
}

